module ApplicationHelper
  attr :tag_stack
  def render_notice_if_notice
    html = ""
    notice_keys = [:error, :success, :info]
    notice_keys.each do |key|
      if flash.key? key
        html += tag_start :div, :class => [:alert, "alert-#{key.to_s}"]
        html += make_tag :a, '&times', :class => :close, "data-dismiss" => :alert
        if flash[key].is_a? Array
          html += make_tag :strong, flash[key][0], :class => "alert-heading" 
          html += flash[key][1]
        else
          html += flash[key]
        end
        html += tag_end
      end
    end
    html
  end

  def make_tag tag, content = '', html_options = {}
    html = "<#{tag} #{make_html html_options}>#{content}</#{tag}>"
  end

  def tag_start tag, html_options = {}
    @tag_stack ||= []
    @tag_stack.push( tag )
    html = "<#{tag} #{make_html html_options}>"
  end

  def tag_end 
    html = "</#{@tag_stack.pop}>"
  end

  def make_html html_options
    tmp = []
    html_options.each do |key, value|
      if value.is_a? Array
        tmp.push("#{key}=\"#{value.join(' ')}\"")
      else
        tmp.push("#{key}=\"#{value}\"")
      end
    end
    return tmp.join(' ')
  end

end
